package com.finch.juc;

import java.util.concurrent.CountDownLatch;

/**
 * @author Levi Wang
 * @create 2021-07-20 22:30
 *
 *
 * 控制线程顺序
 * 业务诉求：希望一个线程等其他线程执行完再执行
 *主线程班长，等待其他六个同学都离开教室后再锁门
 *
 *
 * Count计数 Down倒   CountDown倒计数
 *
 *
 */

public class CountDownLatchDm {

    public static void main(String[] args) throws InterruptedException {

        //第一次模拟，演示错误
//        for (int i = 1; i <= 6; i++) {
//            new Thread(() -> {
//                System.out.println(Thread.currentThread().getName() + "\t离开教室");
//            }, String.valueOf(i)).start();
//        }
//
//        System.out.println("\t班长锁门走人");


        //解决方案：
        CountDownLatch countDownLatch = new CountDownLatch(6); //模拟6个同学
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "\t离开教室");
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println("\t班长锁门走人");




    }

}























